type Toptions_Pform = Toptions__CVP & {
    title?: Toptions_Ppanel$title;
    icon?: Toptions_Ppanel$icon;
    id?: string;
    items?: Array<{ type: string, title: string, options: {} }>;
    buttons?: Array<{}>;
};

class Pform extends __CVP__<Toptions_Pform> {
    // ====================================================== 当前控件特殊实现 ====================================================== //

    // ====================================================== 初始化参数 ====================================================== //
    static defaultOptions: Toptions_Pform = {
    };
    protected override options_default(): Toptions_Pform {
        return Pform.defaultOptions;
    }
    protected override options_handlers(): Toptions_handlers<Toptions_Pform> {
        return {
            title: {
                [U.F.argType.any]: U.F.fn.obj,
                [Etype.obj]: U.F.fn.obj
            },
            icon: {
                [Etype.string]: U.F.fn.obj,
                [Etype.obj]: U.F.fn.obj
            },
            id: {
                [Etype.string]: U.F.fn.obj
            },
            items: {
                [Etype.array]: U.F.fn.obj
            },
            buttons: {
                [Etype.array]: U.F.fn.obj
            },
        };
    }
    protected override overloading_mapping(mapping: Toption_key_to_types_mapping<Toptions_Pform>): Toverloading_mapping<Toptions_Pform> {
        return [
        ];
    }
    protected override options_checker(options: Toptions_Pform): void {
    }

    // ====================================================== 创建实例对象 ====================================================== //
    protected override initializer(): void {
        let ele = this.ele;
        // 图标 & 标题
        if (Object.prototype.hasOwnProperty.call(this.options, 'icon') || Object.prototype.hasOwnProperty.call(this.options, 'title')) {
            this.ele = K.panel({
                icon: this.options.icon,
                title: this.options.title,
                content: ele,
            }).addClass('with-form');
        }

        const ele_form = ele.createElement('form');

        // ID
        if (Object.prototype.hasOwnProperty.call(this.options, 'id')) {
            ele_form.setAttr('id', this.options.id);
        }

        // 输入项栏
        if (Object.prototype.hasOwnProperty.call(this.options, 'items')) {
            const ele_form_items = ele_form.createElement('div').addClass('form-items');
            this.options.items!.forEach(item => {
                const ele_form_item = ele_form_items.createElement('div').addClass('form-item');
                const title = U.is(Etype.string, item.title) ? item.title : '';
                ele_form_item.createElement('div').addClass('form-item-title').setAttr('textContent', title + '：');
                const type = U.is(Etype.string, item.type) ? item.type : DEFAULT;
                const ele_form_input = ele_form_item.createElement('div').addClass('form-item-input');
                if (type !== DEFAULT) {
                    ele_form_input.insertElement((K as any)[item.type](item.options));
                }
            });
        }

        // 按钮栏
        if (Object.prototype.hasOwnProperty.call(this.options, 'buttons')) {
            const ele_form_buttons = ele_form.createElement('div').addClass('form-buttons');
            this.options.buttons!.forEach(buttonOptions => {
                ele_form_buttons.createElement('div').addClass('form-button').insertElement(K.button(buttonOptions));
            });
        }
    }
    protected override afterInitializer(): void {
    }

    // ====================================================== 创建值控件实例对象 ====================================================== //
    public override getValue(): any {
        const form_data: any = {};

        this.ele.query('.' + _CP_.CLASSES.VALUE_PLUGIN, true).forEach(item => {
            if (item.dataset.name) {
                form_data[item.dataset.name] = (item.creator as __CVP__<any>).getValue();
            }
        });

        return form_data;
    }
    public override setValue(value: any): void {
        const t = U.typeof(value);
        switch (t) {
            case Etype.obj:
                for (const key in value) {
                    if (Object.prototype.hasOwnProperty.call(value, key)) {
                        const valueI = value[key];
                        const ele_input = this.ele.query(`[data-name=${key}]`);
                        if (ele_input) {
                            (ele_input.ele()!.creator as __CVP__<any>).setValue(valueI);
                        }
                    }
                }
                break;
            case Etype.DEFAULT:
            case Etype.null:
                this.ele.query('.' + _CP_.CLASSES.VALUE_PLUGIN, true).forEach(item => {
                    if (item.dataset.name) {
                        (item.creator as __CVP__<any>).setValue(DEFAULT);
                    }
                });
                break;
            default:
                break;
        }
    }
}

interface _IK_ {
    // ========= 创建实例对象（通用） ========= //
    /**
     * 创建控件
     * @returns 控件实例对象
     */
    form(): KHTMLElement;

    /**
     * 创建控件
     * @param options 控件参数
     * @returns 控件实例对象
     */
    form(options: Toptions_Pform): KHTMLElement;
}
{
    K.form = (...args: Array<any>) => { return new Pform(...args).ele; };
}
